//
//  SaleReturnViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/20.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "SaleReturnViewController.h"
#import "SaleReturnCell.h"
#import "CompeleteHeadView.h"
#import "CompeleteFooterVeiw.h"
@interface SaleReturnViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation SaleReturnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    [self tableViewAndHeaderView];
    if(!ISNOTNEEDMESSAGEBTN){
        self.isNeedMessageItem = YES;
    }

    [self loadData];

}

-(void)loadData{
    //2.43已完成订单接口
    NSDictionary *paraDic = @{
                              @"userId":USERID,
                              @"state":@""
                              };
    __weak typeof(self)  weakSelf = self;
    [MBProgressHUD showLoadingGif];
    [Tools AFNetworkingRequestTypePostWithUrl:GETCOMPELETEORDERS paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"已完成订单接口---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSArray *data = resultDic[@"data"];
            if([data isKindOfClass:[NSNull class]]){
                [MBProgressHUD dismissHUD];
                return ;
            }
            for(NSDictionary *eventDic in data){
                CompeleteGroup *group = [[CompeleteGroup alloc]init];
                group.storeId  = eventDic[@"storeId"];
                group.storeName = eventDic[@"storeName"];
                group.oId =eventDic[@"oId"];
                group.oPayTime =eventDic[@"oPayTime"];
                group.oStatus =eventDic[@"oStatus"];
                NSArray *pics = eventDic[@"products"];
                NSMutableArray *shopCartModels = [NSMutableArray array];
                for(NSDictionary *dic in pics){
                   CompeleteOrderModel  *model = [[CompeleteOrderModel alloc]init];
                    model.pPic = dic[@"pPic"];
                    model.pName  = dic[@"pName"];
                    model.pPrice  = dic[@"pPrice"];
                    model.pProperty  = dic[@"pProperty"];
//                    model.  = dic[@"pStatus"];
                    [shopCartModels addObject:model];
                }
                group.compeletModels = shopCartModels;
                [weakSelf.dataSource addObject:group];
            }
            if(!weakSelf.dataSource.count){
                [MBProgressHUD dismissHUD];
                [weakSelf.view addSubview:weakSelf.noDataIUmageV];
                return;
            }
            [weakSelf.tableV reloadData];
            weakSelf.tableV.hidden = NO;
            [MBProgressHUD dismissHUD];
        }else{
            [MBProgressHUD dismissHUD];
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

- (void)tableViewAndHeaderView{
    //表视图
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviBarHeight, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight) style:UITableViewStyleGrouped];
    _tableV.showsVerticalScrollIndicator = NO;
    _tableV.backgroundColor = DEF_COLOR242;
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableV];
    _tableV.hidden = YES;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CompeleteGroup *group = self.dataSource[section];
    return group.compeletModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ident=@"actell";
    SaleReturnCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"SaleReturnCell" owner:self options:nil]lastObject];
    }
    CompeleteGroup *group = self.dataSource[indexPath.section];
    cell.model = group.compeletModels[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 98;
}
//自定义头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *headID = @"head";
    CompeleteHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headID];
    if(headView==nil){
        headView = [[[NSBundle mainBundle]loadNibNamed:@"CompeleteHeadView" owner:self options:nil]lastObject];
    }
    headView.group = self.dataSource[section];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 32;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSString *headID = @"footer";
    CompeleteFooterVeiw *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headID];
    if(footerView==nil){
        footerView = [[[NSBundle mainBundle]loadNibNamed:@"CompeleteFooterVeiw" owner:self options:nil]lastObject];
    }
    footerView.group = self.dataSource[section];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 38;
}
@end
